package com.hsnn.medstgmini.util;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hsnn.medstgmini.base.dict.model.DicSys;
import com.hsnn.medstgmini.base.dict.service.DicSysManager;
import com.hsnn.medstgmini.pro.plan.service.PricePlanManager;

/**
 * @category 字典工具类
 * @author 单大伟
 * @date 2015-06-12
 */
@Component
@Scope("singleton")
public class DicSysUtil {
	
	private static final Logger log = Logger.getLogger(DicSysUtil.class);

	private static DicSysManager dicSysManager;
	
	// 大类名表
	private static Set<String> dicClassSet;
	
	// dicClassMap 中包含大类名，和每条大类对应的字典记录
	private static Map<String, List<DicSys>> dicClassMap;
	
	// dicClassCodeMap 中包含大类名，小类名，和含有这条大类名、小类名的字典记录
	private static Map<Map<String, String>, DicSys> dicClassCodeMap;

	@SuppressWarnings("static-access")
	public void setDicSysManager(DicSysManager dicSysManager) {
		this.dicSysManager = dicSysManager;
	}
	
	/*
	 * @category 初始化字典
	 * @author 单大伟
	 * @date 2015-06-12
	 */
	 public static void init() {
		dicClassMap = new HashMap<String, List<DicSys>>();
		dicClassCodeMap = new HashMap<Map<String, String>, DicSys>();
		dicClassSet = new HashSet<String>();
	    
	    if (dicSysManager != null) {
	    	List<DicSys> dicSysList = dicSysManager.getAll();
	    	
			for (DicSys dicSys : dicSysList) {
				dicClassSet.add(dicSys.getClassifyCode());
			}
			
			if (dicSysList != null && !dicSysList.isEmpty()) {
				for (String dicClassName : dicClassSet) {
					//tmpDicSysList 大类目录对应的子类目录列表
					List<DicSys> tmpDicSysList = new LinkedList<DicSys>();
					for (DicSys dicSys: dicSysList) {
						if (dicSys.getClassifyCode().equals(dicClassName)) {
							tmpDicSysList.add(dicSys);
							
							// tmpMap 每条子类目录记录
							Map<String, String> tmpMap = new HashMap<String, String>();
							tmpMap.put(dicClassName, dicSys.getDicCode());
							dicClassCodeMap.put(tmpMap, dicSys);
						}
					}
					dicClassMap.put(dicClassName, tmpDicSysList);
				}
			}
	    } else {
	    	log.error("dicSysManager is null.");
	    }	
	}

	/*
	 * @category 根据字典大类获取字典小类列表
	 * @author 单大伟
	 * @date 2015-06-12
	 * @param classCode  字典大类
	 * @return list<DicSys> 字典小类列表
	 *         null   无法找到小类
	 */
	public static List<DicSys> getDicListByClassCode(String classCode) {
		if (dicClassMap != null && StringUtils.isNotBlank(classCode)) {
			return dicClassMap.get(classCode);
		}
		return null;
	}


	/*
	 * @category 根据字典大类小类获取单条字典信息
	 * @author 单大伟
	 * @date 2015-06-12
	 * @param classCode  字典大类
	 * @param dicCode 字典小类信息
	 * @return DicSys 单条字典信息
	 *         null   无法找到单条字典信息
	 */
	public static DicSys getDicByClassCodeAndDicCode(String classCode, String dicCode) {
		DicSys dicSys = null;
		if (dicClassCodeMap != null && StringUtils.isNotBlank(classCode) && StringUtils.isNotBlank(dicCode)) {
			Map<String, String> subMap = new HashMap<String, String>();
			subMap.put(classCode, dicCode);
			if (subMap != null) {
				dicSys = dicClassCodeMap.get(subMap);
			}
		}
		return dicSys;
	}

	/*
	 * @category 根据字典大类小类获取字典值
	 * @author 单大伟
	 * @date 2015-06-12
	 * @param classCode  字典大类
	 * @param dicCode 字典小类信息
	 * @return String 字典值
	 *         null  无法找到字典值
	 */
	public static String getValueByClassCodeAndDicCode(String classCode, String dicCode) {
		String dicValue = null;
		if (StringUtils.isNotBlank(classCode) && StringUtils.isNotBlank(dicCode)) {
			DicSys dicSys = getDicByClassCodeAndDicCode(classCode, dicCode);
			if (dicSys != null) {
				dicValue = dicSys.getDicName();
			}
		}
		return dicValue;
	}
	
	/*
	 * @category 根据字典大类获取大类名称
	 * @author 单大伟
	 * @date 2015-06-12
	 * @param classCode  字典大类
	 * @return DicSys 字典值
	 *         null  无法找到字典值
	 */
	public static String getClassNameByClassCode(String classCode) {
		
		String className = null;
		//dicClassMap = new HashMap<String, List<DicSys>>();		
		if (StringUtils.isNotBlank(classCode)) {
			List<DicSys> dicSys = dicClassMap.get(classCode);
			if (dicSys != null) {
				className = dicSys.get(0).getClassifyName();
			}
		}
		return className;
	}	
}